﻿@using Fluxor.Blazor.Web.Components;
@using SimpleIdServer.IdServer.Api.Provisioning;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.IdentityProvisioningStore;
@inherits FluxorComponent
@inject IDispatcher dispatcher
@inject DialogService dialogService
@inject NotificationService notificationService

@if (IdProvisioning != null && IdProvisioning.Definition != null)
{
    <EditConfiguration PropertiesSaved=@SaveProperties Name=@IdProvisioning.Definition.OptionsName Values="@IdProvisioning.Values" IsUpdating=@IsUpdating IsAddForm=false></EditConfiguration>
}

@code {
    [Parameter]
    public IdentityProvisioningResult IdProvisioning { get; set; }
    bool IsUpdating { get; set; } = false;

    protected override Task OnAfterRenderAsync(bool firstRender)
    {
        base.OnAfterRenderAsync(firstRender);
        if(firstRender)
        {
            SubscribeToAction<UpdateIdProvisioningPropertiesSuccessAction>((act) =>
            {
                this.IsUpdating = false;
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.IdProvisioningPropertiesUpdated });
                StateHasChanged();
            });
        }

        return Task.CompletedTask;
    }

    void SaveProperties(Dictionary<string, string> properties)
    {
        this.IsUpdating = true;
        var act = new UpdateIdProvisioningPropertiesAction { Id = IdProvisioning.Id, Properties = properties };
        dispatcher.Dispatch(act);
    }
}